﻿Imports System.Data.SqlClient

Public Class KhachHangDAO

    Public Shared Function GetAllKhachHangDAO() As DataTable

        Dim cn As SqlConnection
        cn = SqlDataProvider.ConnectionData()
        Dim ds As ArrayList = New ArrayList()
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "DanhSach_KhachHang"
        cmd.Connection = cn
        Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
        Dim dt As DataTable = New DataTable()
        da.Fill(dt)
        Return dt

    End Function

    Public Shared Sub InsertKhachHangDAO(ByVal kh As KhachHangDTO)

        Dim cn As SqlConnection
        cn = SqlDataProvider.ConnectionData()
        Dim ds As ArrayList = New ArrayList()
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "Insert_KhachHang"
        cmd.Connection = cn
        cmd.Connection.Open()

        Dim para As SqlParameter = New SqlParameter("HoTen", SqlDbType.NVarChar, 50)
        para.Direction = ParameterDirection.Input
        para.Value = kh.HoTen
        cmd.Parameters.Add(para)

        para = New SqlParameter("SoDienThoai", SqlDbType.VarChar, 11)
        para.Direction = ParameterDirection.Input
        para.Value = kh.SoDienThoai
        cmd.Parameters.Add(para)

        para = New SqlParameter("DiaChi", SqlDbType.NVarChar, 50)
        para.Direction = ParameterDirection.Input
        para.Value = kh.DiaChi
        cmd.Parameters.Add(para)

        para = New SqlParameter("Email", SqlDbType.VarChar, 50)
        para.Direction = ParameterDirection.Input
        para.Value = kh.Email
        cmd.Parameters.Add(para)

        para = New SqlParameter("username", SqlDbType.VarChar, 50)
        para.Direction = ParameterDirection.Input
        para.Value = kh.Username
        cmd.Parameters.Add(para)


        cmd.ExecuteNonQuery()
        cmd.Connection.Close()

    End Sub

    Public Shared Sub UpdateKhachHangDAO(ByVal kh As KhachHangDTO)

        Dim cn As SqlConnection
        cn = SqlDataProvider.ConnectionData()
        Dim ds As ArrayList = New ArrayList()
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "Update_KhachHang"
        cmd.Connection = cn
        cmd.Connection.Open()

        Dim para As SqlParameter = New SqlParameter("HoTen", SqlDbType.NVarChar, 50)
        para.Direction = ParameterDirection.Input
        para.Value = kh.HoTen
        cmd.Parameters.Add(para)

        para = New SqlParameter("SoDienThoai", SqlDbType.VarChar, 11)
        para.Direction = ParameterDirection.Input
        para.Value = kh.SoDienThoai
        cmd.Parameters.Add(para)

        para = New SqlParameter("DiaChi", SqlDbType.NVarChar, 50)
        para.Direction = ParameterDirection.Input
        para.Value = kh.DiaChi
        cmd.Parameters.Add(para)

        para = New SqlParameter("Email", SqlDbType.VarChar, 50)
        para.Direction = ParameterDirection.Input
        para.Value = kh.Email
        cmd.Parameters.Add(para)

        para = New SqlParameter("username", SqlDbType.VarChar, 50)
        para.Direction = ParameterDirection.Input
        para.Value = kh.Username
        cmd.Parameters.Add(para)

        cmd.ExecuteNonQuery()
        cmd.Connection.Close()

    End Sub

    Public Shared Sub DeleteKhachHangDAO(ByVal matv As String)

        Dim cn As SqlConnection
        cn = SqlDataProvider.ConnectionData()
        Dim ds As ArrayList = New ArrayList()
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "Delete_KhachHang"
        cmd.Connection = cn
        cmd.Connection.Open()

        Dim para As SqlParameter = New SqlParameter("username", SqlDbType.VarChar, 50)
        para.Direction = ParameterDirection.Input
        para.Value = matv
        cmd.Parameters.Add(para)
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()

    End Sub
End Class
